package bomb.menus;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import bomb.Game;


public class Menu extends BasicGameState {
    
    int ident = 0;
    
    
    Image back = null;
    Image jogar = null;
    Image sound = null;
    Image nosound = null;
    Image music = null;
    Image nomusic = null;
    Image sair = null;


  

    
    float jogar_scale = 1;
    float sound_scale = 1;
    float music_scale = 1;
    float sair_scale = 1;
    
    float scaleStep = 0.001f;
    
    
    public Menu(int id) {
        this.ident = id;
        
    }
    
    @Override
    public int getID() {
        return ident;
    }

    @Override
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
        
        back = new Image("src/images/menufundo.png");
        Image options = new Image("src/images/menu.png");
        nosound = new Image("src/images/nosound.png");
        nomusic = new Image("src/images/nomusic.png");

        
        jogar = options.getSubImage(0, 0, 200, 50);
        
        sound = options.getSubImage(0, 50, 100, 50);
        
        music = options.getSubImage(100, 50, 100, 50);
        
        sair = options.getSubImage(0,100,200,50);
        
        
        
    }

    @Override
    public void render(GameContainer gc, StateBasedGame sbg, Graphics grphcs) throws SlickException {
        back.draw(0,0);
        if(!Game.isSound()){
            nosound.draw(180,180,sound_scale);
        }else{
            sound.draw(180,180,sound_scale);
        }
        if(!Game.isMusic()){
            nomusic.draw(280,180,music_scale);
        }else{
            music.draw(280,180,music_scale);
        }
        jogar.draw(180,280,jogar_scale);
        sair.draw(180,330,sair_scale);
        
        
    }
    
    
    @Override
    public void update(GameContainer gc, StateBasedGame sbg, int i) throws SlickException {
        
        Input input = gc.getInput();
        if(input.isKeyPressed(Input.KEY_ESCAPE)) {
            gc.exit();
        }
        
        int mouseX = input.getMouseX();
        int mouseY = input.getMouseY();
        
        boolean insideJogar = false;
        boolean insideSound = false;
        boolean insideMusic = false;
        boolean insideSair = false;
        
        
        if( (mouseX >= 180 && mouseX < 180 + sound.getWidth()) && (mouseY >= 180 && mouseY < 180 + sound.getHeight()) ) {
            insideSound = true;
        }
        else if( (mouseX >= 280 && mouseX < 280 + music.getWidth()) && (mouseY >= 180 && mouseY < 180 + music.getHeight()) ) {
            insideMusic = true;
        }
        else if( (mouseX >= 180 && mouseX < 180 + jogar.getWidth()) && (mouseY >= 280 && mouseY < 280 + jogar.getHeight()) ) {
            insideJogar = true;

        }
        else if( (mouseX >= 180 && mouseX < 180 + sair.getWidth()) && (mouseY >= 330 && mouseY < 330 + sair.getHeight()) ) {
            insideSair = true;
        }
        
        if(insideJogar) {
            if(jogar_scale < 1.05f) {
                  jogar_scale += scaleStep;
            }
            if ( input.isMousePressed(Input.MOUSE_LEFT_BUTTON) ) {
                Game.clickSound();
                sbg.enterState(Game.LEVELSELECTSTATE);
                gc.reinit();
            }
        }
        else {
            if(jogar_scale > 1.0f) {
            jogar_scale -= scaleStep;
            } 
        } 
        
        
        
        if(insideSound) {
              if(sound_scale < 1.05f) {
                  sound_scale += scaleStep;
              }
              if ( input.isMousePressed(Input.MOUSE_LEFT_BUTTON) ) {
                  if(Game.isSound()){
                      Game.sound();
                  }
                  else{
                      Game.sound();
                      Game.clickSound();
                  }
              }
              
        }
        else {
            if(sound_scale > 1.0f) {
            sound_scale -= scaleStep;
            } 
        }    
        
        if(insideMusic) {
              if(music_scale < 1.05f) {
                  music_scale += scaleStep;
              }
              if ( input.isMousePressed(Input.MOUSE_LEFT_BUTTON) ) {
                  Game.clickSound();
                  Game.music();
              }
        }
        else {
            if(music_scale > 1.0f) {
            music_scale -= scaleStep;
            } 
        }  
        
        
        if(insideSair) {
              if(sair_scale < 1.05f) {
                  sair_scale += scaleStep;
              }
              if ( input.isMousePressed(Input.MOUSE_LEFT_BUTTON) ) {
                  gc.exit();
              }
        }
        else {
            if(sair_scale > 1.0f) {
            sair_scale -= scaleStep;
            } 
        }         
            
        
        
    }
    
}